layui.define(['element', 'jquery', 'common'], function(exports) {
	"use strict";

	var $ = layui.$,
		element = layui.element,
		common = layui.common,
		mainDiv = '.layui-layout',
		nav = '.layui-nav-tree',
		tab = '.layui-tab',
		tabFilter = 'index-tab',
		homeLayId;

	var NAV_ELEM = '.layui-nav',
		NAV_ITEM = 'layui-nav-item',
		NAV_BAR = 'layui-nav-bar',
		NAV_TREE = 'layui-nav-tree',
		NAV_CHILD = 'layui-nav-child',
		NAV_MORE = 'layui-nav-more',
		NAV_ANIM = 'layui-anim layui-anim-upbit',
		THIS = 'layui-this',
		SHOW = 'layui-show';

	var index = {
		init: function() {
			//导航事件绑定
			$(mainDiv).find(nav).find('a[index-url]').click(function() {
				var e = $(this),
					text = e.text().trim(),
					indexUrl = e.attr('index-url');
				index.tabAdd(text, indexUrl);
			})
			//index界面事件绑定
			$('*[index-event]').click(function() {
				var e = $(this),
					event = e.attr('index-event');
				index[event].call(e);
			})
			//监听tab删除
			element.on('tabDelete(' + tabFilter + ')', function(data) {
				var triggerId = data.index > 0 ? data.index - 1 : 0;
				data.elem.find('.layui-tab-title li').each(function(i, item) {
					if (triggerId == i) {
						var layId = $(item).attr('lay-id');
						index.clickNav(layId);
					}
				})
			});
			//监听tab切换
			element.on('tab(' + tabFilter + ')', function(data) {
				index.navThis($(this).attr('lay-id'));
			});
		},
		//主页
		home: function(title, url) {
			index.tabAdd(title, url);
			index.navThis(url);
		},
		//根据layId点击导航
		clickNav: function(layId) {
			$(mainDiv).find(nav).find('a[index-url]').each(function(index, item) {
				var e = $(item),
					url = e.attr('index-url');
				if (layId == url) {
					e.trigger('click');
				}
			})
		},
		//新增一个Tab项
		tabAdd: function(title, url) {
			var isAdd = true;
			$(tab).find('.layui-tab-title li').each(function(i, item) {
				var layId = $(this).attr('lay-id');
				if (url == layId) {
					isAdd = false;
				}
			})
			if (isAdd) {
				element.tabAdd(tabFilter, {
					title: title,
					content: '<iframe src="' + url + '" frameborder="0" class="index-iframe"></iframe>',
					id: url
				})
			}
			index.tabChange(url);
		},
		//删除指定Tab项
		tabDelete: function(layId) {
			element.tabDelete(tabFilter, layId);
		},
		//切换到指定Tab项
		tabChange: function(layId) {
			element.tabChange(tabFilter, layId);
		},
		//刷新当前Tab
		refresh: function() {
			var tab = $('.index-body .layui-tab-item.layui-show');
			var currentIframe = tab.find(".index-iframe");
			if (currentIframe && currentIframe[0]) {
				try {
					currentIframe[0].contentWindow.location.reload(true);
				} catch (e) {
					currentIframe.attr('src', currentIframe.attr('src'));
				}
			}
		},
		//注销
		signout: function() {
			common.confirm('确定注销吗？', function() {
				window.location.replace('login.html');
			})
		},
		//选中当前导航
		navThis: function(layId) {
			var othis;
			$(mainDiv).find(nav).find('a[index-url]').each(function(index, item) {
				var e = $(item),url = e.attr('index-url');
				if (layId == url) {
					othis = e;
				}
			})
			var parents = othis.parents(NAV_ELEM),
				filter = parents.attr('lay-filter'),
				parent = othis.parent(),
				child = othis.siblings('.' + NAV_CHILD),
				unselect = typeof parent.attr('lay-unselect') === 'string';

			if (!(othis.attr('href') !== 'javascript:;' && othis.attr('target') === '_blank') && !unselect) {
				if (!child[0]) {
					parents.find('.' + THIS).removeClass(THIS);
					parent.addClass(THIS);
				}
			}

			//如果是垂直菜单
			if (parents.hasClass(NAV_TREE)) {
				child.removeClass(NAV_ANIM);
				//如果有子菜单，则展开
				if (child[0]) {
					parent[child.css('display') === 'none' ? 'addClass' : 'removeClass'](NAV_ITEM + 'ed');
					if (parents.attr('lay-shrink') === 'all') {
						parent.siblings().removeClass(NAV_ITEM + 'ed');
					}
				}
			}
		}
	}
	index.init();

	exports('index', index);
});
